<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_structure} function plugin
     *
     * Type:     function
     * Name:     load_structure
     * Purpose:  Load the data tree into a predefined variable
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * template parameters:
     *   
     *   * var (string) -- name of variable where to load the 
     *                     data tree to
     *                     >> default: tree
     *
     *   * level (int)  -- maximum depth to go to 
     *                     >> default: 3
     *
     * @return string
     */
    function smarty_function_load_structure($params, &$smarty){
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'tree';
        }else{
            $var = $params['var'];
        }

        //depth of tree
        if(empty($params['level'])){
            $level = 3;
        }else{
            $level = (int)$params['level'];
        }
        
        //get id of listing folder
        $type = $db->getOne("SELECT id FROM datatype WHERE codeset = 'ifolder' AND folder = TRUE");
        
        //get tree
        $tree = new tree($db,'structure AS s LEFT JOIN objectmap AS om ON s.id = om.nodeid',null,'s.id AS id, s.level AS level, get_path(id) as path, om.title AS title, om.published_pages AS published_pages'); 
        $smarty->assign($var,$tree->getTree(null,$level,false,'wf_state = 1 AND s.node_type = '.$type));
    }
?>